<?php

namespace app\model;

use think\Model;

class SystemNotice extends Model
{
    /**
     * 系统公告
     */

    protected $createTime='create_date';

    public function getExeStartDateAttr($name)
    {
        if (empty($name) || $name == '0000-00-00 00:00:00') return '';
        return $name;
    }

    public function getExeEndDateAttr($name)
    {
        if (empty($name) || $name == '0000-00-00 00:00:00') return '';
        return $name;
    }

    public function addDataModel($param)
    {
        return self::create ($param);
    }

    public function delId(string $datum): bool
    {
        return self::destroy($datum);
    }

    /**
     * 查询分页数据
     * @param $page
     * @param $pageNum
     * @param $type
     * @param $where
     * @return \app\model\SystemNotice[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPageDataModel($page, $pageNum, $type, $where){
        return $this->where (['type'=>$type])->where ($where)->page ($page,$pageNum)->order('sort DESC')->select();
    }

    public function getDataCountModel($type,$where){
        return $this->where (['type'=>$type])->where ($where)->order('sort DESC')->count();
    }

    /**
     * 查询全部数据
     * @param $type
     * @param string $where
     * @return \app\model\SystemNotice[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDataModel($type, string $where='')
    {
        return $this->where (['type'=>$type])->where ($where)->order('sort ASC')->select();
    }

    public function getBetweenDateDataModel()
    {
//        $data = $this->where (['type'=>1])->whereBetweenTimeField('exe_start_date', 'exe_end_date')->select ();
        $data = $this->where (['type'=>1])->select ()->toArray ();

        $result = [];
        foreach ($data as $datum) {
            if ($datum['exe_start_date'] == '' || $datum['exe_end_date'] == ''){
                $result [] = $datum;
            }else{
                if (strtotime ($datum['exe_start_date']) <= time() && strtotime ($datum['exe_end_date']) >= time()){
                    $result[] = $datum;
                }
            }
        }
        return $result;
    }

    public function editId($param)
    {
        return self::update ($param);
    }

    public function getCountModel()
    {
        return $this->count();
    }
}